#!/bin/bash

REPOSITORY_PATH='registry.cn-shenzhen.aliyuncs.com/yueyang-cloud/'
TAG='1.0.0'

package() {
  mvn clean package -Dmaven.test.skip -f ../pom.xml
  cd ../yueyang-ui || exit
  npm install --registry=https://registry.npmmirror.com --unsafe-perm
  npm run build:prod
}

move() {
  # 认证服务
  YUEYANG_AUTH='../yueyang-auth/target/yueyang-auth.jar'
  if [ -f "${YUEYANG_AUTH}" ]; then
    if [ ! -d './yueyang-auth/java' ]; then
      mkdir ./yueyang-auth/java
    fi
    mv ${YUEYANG_AUTH} ./yueyang-auth/java
  fi
  # 网关服务
  YUEYANG_GATEWAY='../yueyang-gateway/target/yueyang-gateway.jar'
  if [ -f "${YUEYANG_GATEWAY}" ]; then
    if [ ! -d './yueyang-gateway/java' ]; then
      mkdir ./yueyang-gateway/java
    fi
    mv ${YUEYANG_GATEWAY} ./yueyang-gateway/java
  fi
  # 所有module模块
  # shellcheck disable=SC2012
  YUEYANG_MODULES=$(ls -lh yueyang-modules | awk '{print $9}')
  for YUEYANG_MODULE in ${YUEYANG_MODULES}; do
    MODULE_JAR="../yueyang-modules/${YUEYANG_MODULE}/target/${YUEYANG_MODULE}.jar"
    MODULE_DIR="./yueyang-modules/${YUEYANG_MODULE}/java"
    if [ -f "${MODULE_JAR}" ]; then
      if [ ! -d "${MODULE_DIR}" ]; then
        mkdir "${MODULE_DIR}"
      fi
      mv "${MODULE_JAR}" "${MODULE_DIR}"
    fi
  done
  # 监控服务
  YUEYANG_MONITOR='../yueyang-visual/yueyang-monitor/target/yueyang-monitor.jar'
  if [ -f "${YUEYANG_MONITOR}" ]; then
    if [ ! -d './yueyang-visual/yueyang-visual-monitor/java' ]; then
      mkdir ./yueyang-visual/yueyang-visual-monitor/java
      mv ${YUEYANG_MONITOR} ./yueyang-visual/yueyang-visual-monitor/java
    fi
  fi
  # 前端服务
  UI_DIR='../yueyang-ui/dist'
  if [ -d "${UI_DIR}" ]; then
    if [ -d "./yueyang-ui/dist" ]; then
      rm -rf ./yueyang-ui/dist
    fi
    mv ${UI_DIR} ./yueyang-ui
  fi
  # 数据库
  SQL_DIR='./yueyang-mysql/sql'
  if [ -d "${SQL_DIR}" ]; then
    rm -rf "${SQL_DIR}"
  fi
  cp -r ../sql ./yueyang-mysql
  cd yueyang-mysql/sql || exit
  tar -zcvf init_sql.tar.gz *.sql
  rm -rf *.sql
}

build() {
  CURRENT_DIR=$(pwd)
  DOCKERFILES=$(find . -name Dockerfile)
  for DOCKERFILE in ${DOCKERFILES}; do
    BUILD_DIR="${DOCKERFILE%/*}"
    MODULE_NAME=${BUILD_DIR##*-}
    BUILD_DIR="${CURRENT_DIR}${BUILD_DIR#*.}"
    cd "${BUILD_DIR}" || exit
    docker build -t "${REPOSITORY_PATH}${MODULE_NAME}:${TAG}" .
  done
}

push() {
  IMAGE_LIST=$(docker images | grep "${REPOSITORY_PATH}" | awk '{print $1":"$2}')
  if [ -n "${IMAGE_LIST}" ]; then
    for IMAGE in ${IMAGE_LIST}; do
      docker push "${IMAGE}"
    done
  fi
}

clean() {
  mvn clean -f ../pom.xml
  ALL_JAVA_DIRS=$(find . -name java)
  if [ -n "${ALL_JAVA_DIRS}" ]; then
    for JAVA_DIR in ${ALL_JAVA_DIRS}; do
      rm -rf "${JAVA_DIR}"
    done
  fi
  if [ -d "../yueyang-ui/dist" ]; then
    rm -rf ../yueyang-ui/dist
  fi
  if [ -d "./yueyang-ui/dist" ]; then
    rm -rf ./yueyang-ui/dist
  fi
  if [ -d "./yueyang-mysql/sql" ]; then
      rm -rf ./yueyang-mysql/sql
  fi
  IMAGE_LIST=$(docker images | grep "${REPOSITORY_PATH}" | awk '{print $1":"$2}')
  if [ -n "${IMAGE_LIST}" ]; then
    for IMAGE in ${IMAGE_LIST}; do
      docker rmi -f "${IMAGE}"
    done
  fi
}

case $1 in
"package")
  package
  ;;
"move")
  move
  ;;
"build")
  build
  ;;
"push")
  push
  ;;
"clean")
  clean
  ;;
*)
  echo "未找到命令$1，命令执行格式："
  echo "yueyang.sh command"
  echo "command: {package|move|build|push|clean}"
  echo "package: 将项目打包"
  echo "move: 将打包好的项目移动到构建空间"
  echo "build: 将项目打成docker包"
  echo "push: 推送构建好的docker包"
  echo "clean: 清理构建残余"
  ;;
esac
